package com.ag.hmk.action;

import javax.annotation.Resource;

import com.ag.bean.TbGoods;
import com.ag.bean.TbGoodstype;
import com.ag.bean.TbUnit;
import com.ag.common.BaseAction;
import com.ag.common.IDUtil;
import com.ag.common.IIDService;
import com.ag.hmk.service.impl.GoodsServImpl;
import com.ag.hmk.service.impl.GoodtypeServImpl;
import com.ag.hmk.service.impl.UnitServImpl;
/**
 * 
 * @author hmk
 * 保存物资
 */
public class SaveGoodsAction extends BaseAction {

	private static final long serialVersionUID = 1656567L;
	
	private GoodsServImpl goodsServImpl;
	private GoodtypeServImpl goodtypeServImpl;
	private UnitServImpl unitServImpl;
	@Resource(name = "iDServiceImpl")
	private IIDService idservice;
	
	private String gID;
	private String gtypeID1;
	private String gunitID1;
	private String gname1;
	private String gdetail1;
	private String ajaxString;
	
	/*
	 * 保存物资
	 */
	public String saveGood()throws Exception{
		try {
			TbUnit gUnit = unitServImpl.findByID(gunitID1);
			TbGoodstype gtype = goodtypeServImpl.findByID(gtypeID1);
			if (gUnit!=null && gname1!=null && gname1.length()>0  ) {
				TbGoods goods = new TbGoods();
				String gID = idservice.findTablePK(IDUtil.TB_GOODS);
				goods.setGoodsId(gID);
				goods.setGoodsName(gname1);
				goods.setGoodsDetail(gdetail1);
				goods.setTbGoodstype(gtype);
				goods.setTbUnit(gUnit);
				
				goodsServImpl.save(goods);
				this.setAjaxString("1");
				System.out.println("-------------物资保存成功"+gtype.getGtName());
			} else{
				System.out.println("不能为空");
				this.setAjaxString("0");
			}
		} catch (Exception e) {
			System.out.println("---保存物资发生异常----");
			this.setAjaxString("0");
		}
		
		return "saveGoodOK";
	}
	
	/*
	 * 物资更新
	 */
	public String updateGood()throws Exception{
		try {
			TbUnit gUnit = unitServImpl.findByID(gunitID1);
			TbGoodstype gtype = goodtypeServImpl.findByID(gtypeID1);
			TbGoods goods = goodsServImpl.findByID(gID);
			if (goods!=null && gUnit!=null && gname1!=null && gname1.length()>0  ) {
				goods.setGoodsName(gname1);
				goods.setGoodsDetail(gdetail1);
				goods.setTbGoodstype(gtype);
				goods.setTbUnit(gUnit);
				
				goodsServImpl.save(goods);
				this.setAjaxString("1");
				System.out.println("=============物资保存成功"+gtype.getGtName());
			} else{
				System.out.println("不能为空");
				this.setAjaxString("0");
			}
		} catch (Exception e) {
			System.out.println("---更新物资发生异常----");
			this.setAjaxString("0");
		}
		return "saveGoodOK";
	}

	public String getAjaxString() {
		return ajaxString;
	}

	public void setAjaxString(String ajaxString) {
		this.ajaxString = ajaxString;
	}

	public void setGoodsServImpl(GoodsServImpl goodsServImpl) {
		this.goodsServImpl = goodsServImpl;
	}

	public void setGoodtypeServImpl(GoodtypeServImpl goodtypeServImpl) {
		this.goodtypeServImpl = goodtypeServImpl;
	}

	public void setUnitServImpl(UnitServImpl unitServImpl) {
		this.unitServImpl = unitServImpl;
	}

	public void setGtypeID1(String gtypeID1) {
		this.gtypeID1 = gtypeID1;
	}

	public void setGunitID1(String gunitID1) {
		this.gunitID1 = gunitID1;
	}

	public void setGname1(String gname1) {
		this.gname1 = gname1;
	}

	public void setGdetail1(String gdetail1) {
		this.gdetail1 = gdetail1;
	}

	public void setGID(String gid) {
		gID = gid;
	}

}
